/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jcode.netty.server;

import com.jcode.netty.core.NettySystemConfig;
import com.jcode.netty.protocol.Protocol;
import com.jcode.netty.serialize.SerializeType;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shijia.wxr
 */
public class ServerConfig implements Cloneable {
	private String bindIp = "127.0.0.1";
	private int listenPort = 8888;
	private List<Integer> listenPortList = new ArrayList<Integer>(10);
	private int serverBossThreads = 1;
	private int serverWorkerThreads = Runtime.getRuntime().availableProcessors();
	private int serverCallbackExecutorThreads = 0;
	private int serverOnewaySemaphoreValue = 2010000;
	private int serverAsyncSemaphoreValue = 2010000;
	private int serverChannelMaxIdleTimeSeconds = 180;

	private int serverSocketSndBufSize = NettySystemConfig.SocketSndbufSize;
	private int serverSocketRcvBufSize = NettySystemConfig.SocketRcvbufSize;
	private boolean serverPooledByteBufAllocatorEnable = true;

	private int maxConnectionSize = 1010000;

	private Protocol protocol;

	private SerializeType serializeType = SerializeType.JSON;

	/**
	 * make make install
	 * 
	 * 
	 * ../glibc-2.10.1/configure \ --prefix=/usr \ --with-headers=/usr/include \
	 * --host=x86_64-linux-gnu \ --build=x86_64-pc-linux-gnu \ --without-gd
	 */
	private boolean useEpollNativeSelector = false;

	public String getBindIp() {
		return bindIp;
	}

	public void setBindIp(String bindIp) {
		this.bindIp = bindIp;
	}

	public int getListenPort() {
		return listenPort;
	}

	public void setListenPort(int listenPort) {
		this.listenPort = listenPort;
	}


	public int getServerBossThreads() {
		return serverBossThreads;
	}

	public void setServerBossThreads(int serverBossThreads) {
		this.serverBossThreads = serverBossThreads;
	}

	public int getServerWorkerThreads() {
		return serverWorkerThreads;
	}

	public void setServerWorkerThreads(int serverWorkerThreads) {
		this.serverWorkerThreads = serverWorkerThreads;
	}

	public int getServerOnewaySemaphoreValue() {
		return serverOnewaySemaphoreValue;
	}

	public void setServerOnewaySemaphoreValue(int serverOnewaySemaphoreValue) {
		this.serverOnewaySemaphoreValue = serverOnewaySemaphoreValue;
	}

	public int getServerCallbackExecutorThreads() {
		return serverCallbackExecutorThreads;
	}

	public void setServerCallbackExecutorThreads(int serverCallbackExecutorThreads) {
		this.serverCallbackExecutorThreads = serverCallbackExecutorThreads;
	}

	public int getServerAsyncSemaphoreValue() {
		return serverAsyncSemaphoreValue;
	}

	public void setServerAsyncSemaphoreValue(int serverAsyncSemaphoreValue) {
		this.serverAsyncSemaphoreValue = serverAsyncSemaphoreValue;
	}

	public int getServerChannelMaxIdleTimeSeconds() {
		return serverChannelMaxIdleTimeSeconds;
	}

	public void setServerChannelMaxIdleTimeSeconds(int serverChannelMaxIdleTimeSeconds) {
		this.serverChannelMaxIdleTimeSeconds = serverChannelMaxIdleTimeSeconds;
	}

	public int getServerSocketSndBufSize() {
		return serverSocketSndBufSize;
	}

	public void setServerSocketSndBufSize(int serverSocketSndBufSize) {
		this.serverSocketSndBufSize = serverSocketSndBufSize;
	}

	public int getServerSocketRcvBufSize() {
		return serverSocketRcvBufSize;
	}

	public void setServerSocketRcvBufSize(int serverSocketRcvBufSize) {
		this.serverSocketRcvBufSize = serverSocketRcvBufSize;
	}

	public boolean isServerPooledByteBufAllocatorEnable() {
		return serverPooledByteBufAllocatorEnable;
	}

	public void setServerPooledByteBufAllocatorEnable(boolean serverPooledByteBufAllocatorEnable) {
		this.serverPooledByteBufAllocatorEnable = serverPooledByteBufAllocatorEnable;
	}

	public boolean isUseEpollNativeSelector() {
		return useEpollNativeSelector;
	}

	public void setUseEpollNativeSelector(boolean useEpollNativeSelector) {
		this.useEpollNativeSelector = useEpollNativeSelector;
	}

	public Protocol getProtocol() {
		return protocol;
	}

	public void setProtocol(Protocol protocol) {
		this.protocol = protocol;
	}

	public SerializeType getSerializeType() {
		return serializeType;
	}

	public void setSerializeType(SerializeType serializeType) {
		this.serializeType = serializeType;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return (ServerConfig) super.clone();
	}

	public int getMaxConnectionSize() {
		return maxConnectionSize;
	}

	public void setMaxConnectionSize(int maxConnectionSize) {
		this.maxConnectionSize = maxConnectionSize;
	}


	public List<Integer> getListenPortList() {
		return listenPortList;
	}

	public void setListenPortList(List<Integer> listenPortList) {
		this.listenPortList = listenPortList;
	}

	public void addListenPort(Integer listenPort) {
		listenPortList.add(listenPort);
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
